﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Security.Permissions;
using Bussiness;
using System.Data;

namespace EWebService {
    /// <summary>
    /// Summary description for Service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService {

        [WebMethod]
        public string HelloWorld() {
            return "Hello World";
        }

        [WebMethod(EnableSession = true)]
        [SecurityPermission(SecurityAction.Demand, ControlDomainPolicy = true)]
        public string WSLogin(String partnerID, String partnerPwd) {
            //Session.Clear();
            PartnerBuss buss = new PartnerBuss();
            //is partner
            int id = buss.isPartner(partnerID, partnerPwd);
            if (id > -1) {
                if (Session["SID"] != null)
                    return Session.SessionID;

                Session.Add("SID", Session.SessionID);
                //Session["SID"] = Session.SessionID;
                //write to loginlog
                buss.writeLogLogin(Session.SessionID, id);
                return Session.SessionID;
                //return "s";
            }
            else {
                return "-1";
            }
        }

        [WebMethod]
        public DataTable Category(String parentCatID = "") {
            DataTable table = new DataTable();
            CategoryBuss buss = new CategoryBuss();
            if (parentCatID.Equals(""))
                table = buss.getCategories();
            else {
                int parentID = 100;
                if (parentCatID.Equals("1"))
                    parentID = 100;
                else if (parentCatID.Equals("2"))
                    parentID = 101;
                else
                    parentID = int.Parse(parentCatID);
                
                table = buss.getCategories(parentID);
            }
            return table;
        }

        [WebMethod]
        public DataTable Product(String catID) {
            try {
                int id = int.Parse(catID);
                ProductBuss buss = new ProductBuss();
                return buss.wsGetProducts(id);
            }
            catch (Exception ex) {
                return null;
            }
        }

        [WebMethod(EnableSession = true)]
        public String RegisterTrans(String SID, DataTable registerList) {
            //check session
           // if (!SID.Equals(Session["SID"])) //is not login or session expires
           //     return "-1";

            //register trans
            TransactionBuss buss = new TransactionBuss();
            return buss.registerTrans(SID, registerList);
        }

        [WebMethod(EnableSession = true)]
        public String EndUpdateInventory(String SID, String TransID, String Msg) {
            try {
                EndUpdateInventoryBuss buss = new EndUpdateInventoryBuss();
                if (buss.add(SID, TransID, Msg))
                    return "0";
                else
                    return "-1";
            }
            catch (Exception ex) {
                return "2";
            }
        }
    }
}
